const ffi = require("ffi-napi");
const path = require("path");
// const { app } = require("electron");
const log = require("electron-log");

exports.useAdd = function useAdd() {
  try {
    // 定义 DLL 文件中的函数类型和参数类型
    // eslint-disable-next-line no-undef
    let dllPath = path.join(__static, "sdk", "demodll.dll");

    if (process.env.NODE_ENV !== "development") {
      dllPath = path.join(
        process.resourcesPath,
        "app.asar.unpacked",
        "sdk",
        "demodll.dll"
      );
    }

    log.info("demodll.dll dllPath", dllPath);
    const add = ffi.Library(dllPath, {
      add: ["int", ["int", "int"]],
    });

    // nodejs --> dll

    // 初始化后的对象

    // 调用函数并弹出结果
    const result = add.add(2, 3);

    log.info("demodll.dll 执行成功，结果是：", result);
    return result;
  } catch (error) {
    log.error("demodll.dll 执行失败");
    log.error("demodll.dll 错误信息", error.message);
    return "error";
  }
};
